home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / ROM_Kernel_Manuals / Lib_examples / port1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-20  |  2.5 KB  |  72 lines

  1. ;/* port1.c - Execute me to compile with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 port1.c
  3. Blink FROM LIB:c.o,port1.o TO port1 LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5.  
  6. * port1.c - port and message example, run at the same time as port2.c
  7. */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/ports.h>
  11. #include <dos/dos.h>
  12. #include <clib/exec_protos.h>
  13. #include <clib/alib_protos.h>
  14. #include <stdio.h>
  15.  
  16. #ifdef LATTICE
  17. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL-C handling */
  18. int chkabort(void) {return(0);}
  19. #endif
  20.  
  21. struct XYMessage {
  22.     struct Message xym_Msg;
  23.     WORD           xy_X;
  24.     WORD           xy_Y;
  25. };
  26.  
  27. void main(int argc, char **argv)
  28. {
  29.     struct MsgPort *xyport;
  30.     struct XYMessage *xymsg;
  31.     ULONG portsig, usersig, signal;
  32.     BOOL ABORT = FALSE;
  33.  
  34.     if (xyport = CreatePort("xyport", 0))
  35.     {
  36.         portsig = 1 << xyport->mp_SigBit;       /* Give user a `break' signal. */
  37.         usersig = SIGBREAKF_CTRL_C;
  38.  
  39.         printf("Start port2 in another shell.  CTRL-C here when done.\n");
  40.         do
  41.         {                                                 /* port1 will wait forever and reply   */
  42.             signal = Wait(portsig | usersig);             /* to messages, until the user breaks. */
  43.  
  44.                                    /* Since we only have one port that might get messages we     */
  45.             if (signal & portsig)  /* have to reply to, it is not really necessary to test for   */
  46.             {                      /* the portsignal. If there is not message at the port, xymsg */
  47.  
  48.                 while(xymsg = (struct XYMessage *)GetMsg(xyport))        /* simply will be NULL. */
  49.                 {
  50.                     printf("port1 received: x = %d y = %d\n", xymsg->xy_X, xymsg->xy_Y);
  51.  
  52.                     xymsg->xy_X += 50;       /* Since we have not replied yet to the owner of    */
  53.                     xymsg->xy_Y += 50;       /* xymsg, we can change the data contents of xymsg. */
  54.  
  55.                     printf("port1 replying with: x = %d y = %d\n", xymsg->xy_X, xymsg->xy_Y);
  56.                     ReplyMsg((struct Message *)xymsg);
  57.                 }
  58.             }
  59.  
  60.             if (signal & usersig)                                    /* The user wants to abort. */
  61.             {
  62.                 while(xymsg = (struct XYMessage *)GetMsg(xyport))    /* Make sure port is empty. */
  63.                     ReplyMsg((struct Message *)xymsg);
  64.                 ABORT = TRUE;
  65.             }
  66.         }
  67.         while (ABORT == FALSE);
  68.             DeletePort(xyport);
  69.         }
  70.     else printf("Couldn't create 'xyport'\n");
  71. }
  72.